// 引入 uni-ajax 模块
import ajax from '@/uni_modules/u-ajax'
import md5 from '@/common/md5.js';
import configs from '@/common/config.js';
//无需登录接口
const notLogin = ["/api/applet/user/login", "/api/applet/user/register", "/api/app/advert/list", "/api/app/user/get",
	"/api/applet/user/auth/phone", "/api/applet/user/getWxOpenidInfo", "/api/app/richtext/list",
	"/api/app/richtext/get", "/api/common/fileUpload", "/api/common/randcode/sendSmsv1", "/api/app/config/get",
	"/api/app/user/logout",
	"/api/app/goods/pages",
	"/api/app/goods/get",
	"/api/app/goods/recordPage",
	"/api/app/goods/getClass",
	"/api/app/goods/getShop",
	"/api/app/goods/getGoodsAndClass",
	"/api/app/article/list", "/api/app/article/get", "/api/app/article/comment/list",
	"/manage/petGoods/list",
	"/manage/petGoods/detail",
	"/manage/member/register",
	"/manage/member/login",
	"/manage/member/getMember",
];
//需要系统提示并阻止的接口-如营业停止时禁止添加购物车等
const disApi = ["/api/app/goods/addCar", "/api/app/goods/pay"];
// 创建请求实例
const instance = ajax.create({
	// 初始配置
	baseURL: configs.baseUrl,
	custom: {
		ShowLoading: true,
		LoadingMask: true
	}
})


// 添加请求拦截器
instance.interceptors.request.use(
	
	config => {
		// 在发送请求前做些什么
		var uid = uni.getStorageSync("uid");
		var token = uni.getStorageSync("token");
		var sysConfig = uni.getStorageSync("config");
		var time = (new Date().getTime() / 1000).toFixed()
		var secret = "";
		if (notLogin.indexOf(config.url) != -1) {
			secret = md5.hex_md5('erlang' + time);
		} else {
			//如果是休息中
			if (sysConfig.sysNotice && sysConfig.sysNotice.indexOf('休息') != -1 &&
				disApi.indexOf(config.url) != -1) {
				return false
			}
			secret = md5.hex_md5('erlang' + time + "" + uid + token)
		}
		if (config.custom.ShowLoading) {
			uni.showLoading({
				title: config.custom.LoadingText || '正在加载',
				mask: config.custom.LoadingMask || false
			});
		}
		config.header.access_token = token;
		config.header.Authorization = "Bearer "+uni.getStorageSync("token");
		config.url = config.baseURL + config.url + '?time=' + time + '&secret=' + secret;
		return config
	},
	
	error => {
		uni.showToast({
			title: "网络连接错误"
		})
		// 对请求错误做些什么
		return Promise.reject(error)
	}
)

// 添加响应拦截器
instance.interceptors.response.use(

	res => {
		if (res.config.custom.ShowLoading) {
			// uni.hideLoading()
			setTimeout(function() {
				uni.hideLoading()
			}, 200)
		}

		// if 状态码是否正常
		if (res.statusCode == 200) {
			let result = res.data;
			// if 与后台规定的成功码是否正常
			if (result.code == 20000) {
				return result.data;
			}
			// else if (result.code == 20004) {

			// 	uni.showToast({
			// 		title: "用户不存在"
			// 	})
			// 	setTimeout(function() {
			// 		//跳转注册
			// 	}, 1500);
			// 	return Promise.reject(result))
			// } 
			else if (result.code == 20006) {
				uni.showToast({
					title: "登录失败或者过期!"
				})
				uni.navigateTo({
					url: "/pagesOne/login/login"
				})
				return Promise.reject(result)
			} else if (result.code == 20007) {
				uni.clearStorageSync();
				uni.showToast({
					title: "登录失败或者过期!"
				})
				uni.navigateTo({
					url: "/pagesOne/login/login"
				})
				return Promise.reject(result)
			} else if (result.code == 50000) {
				uni.showToast({
					title: result.message
				})
				return Promise.reject(result)
			} else if (result.code == 0) {
				uni.showToast({
					title: "未知错误!"
				})
				return Promise.reject(result)
			} else {
				uni.showToast({
					title: result.msg
				})
				return Promise.reject(result)
			}
		} else {
			// let errorMessage = codeMessage[res.statusCode] ? codeMessage[res.statusCode] : res.statusCode;
			uni.showToast({
				title: "网络错误"
			})
		}
		return res
	},
	error => {
		var sysConfig = uni.getStorageSync("config");
		if (sysConfig.sysNotice && sysConfig.sysNotice.indexOf('休息') != -1) {
			uni.showModal({
				title: "通知",
				content: sysConfig.sysNotice,
				showCancel: false
			})
		} else {
			uni.showToast({
				title: "网络连接错误"
			})
		}

		// 对响应错误做些什么
		return Promise.reject(error)
	}
)

// 导出 create 创建后的实例
export default instance
